﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using SilverlightOracle.WCFService;
using System.Text.RegularExpressions;

namespace SilverlightOracle
{
    public partial class QuanTriThiSinh : Page
    {
        ServiceClient webservice = new ServiceClient();
        public QuanTriThiSinh()
        {
            InitializeComponent();
            webservice.GetTruongCompleted += new EventHandler<GetTruongCompletedEventArgs>(webservice_GetTruongCompleted);
            webservice.GetTruongAsync();
            radioButton1.IsChecked = true;
            dgView.Visibility = Visibility.Collapsed;
        }

        void webservice_GetTruongCompleted(object sender, GetTruongCompletedEventArgs e)
        {
            comboBox1.ItemsSource = e.Result;
            comboBox1.SelectedIndex = 0;
        }
        void ShowThiSinh()
        {
            webservice.ShowThiSinhCompleted +=new EventHandler<ShowThiSinhCompletedEventArgs>(ShowThiSinhCompleted);
            webservice.ShowThiSinhAsync();
        }
        void ShowThiSinhCompleted(object sender, ShowThiSinhCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                dgView.Visibility = Visibility.Visible;
                dgView.ItemsSource = e.Result;
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void dgView_GotFocus(object sender, RoutedEventArgs e)
        {
            var cw = new SubQuanTriThiSinh();
            cw.SelectedUser = dgView.SelectedItem as detailthisinh;
            cw.Show();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ShowThiSinh();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate()) return;
            else
            {
                webservice.SearchDetailThiSinhCompleted += new EventHandler<SearchDetailThiSinhCompletedEventArgs>(webservice_SearchDetailThiSinhCompleted);
                if (radioButton1.IsChecked == true)
                {
                    webservice.SearchDetailThiSinhAsync(comboBox1.SelectedValue.ToString(), textBox1.Text, "", 0);
                }
                else
                {
                    if (radioButton2.IsChecked == true)
                    {
                        webservice.SearchDetailThiSinhAsync(comboBox1.SelectedValue.ToString(), "", textBox1.Text, 0);
                    }
                    else
                    {
                        webservice.SearchDetailThiSinhAsync(comboBox1.SelectedValue.ToString(), "", "", Convert.ToInt64(textBox1.Text));
                    }
                }
            }
        }

        void webservice_SearchDetailThiSinhCompleted(object sender, SearchDetailThiSinhCompletedEventArgs e)
        {
            if (e.Result.Count != 0)
            {
                Error.Content = "";
                dgView.Visibility = Visibility.Visible;
                dgView.ItemsSource = e.Result;
            }
            else
            {
                Error.Content = "Không tìm thấy thí sinh . Yêu cầu kiểm tra lại từ khóa !";
            }
            
        }
        bool Validate()
        {
            if (textBox1.Text.Length == 0)
            {
                Error.Content = "Từ khóa rỗng";
                textBox1.Focus();
                return false;
            }
            if (radioButton1.IsChecked == true)
            {
                if (!Regex.IsMatch(textBox1.Text, @"^[a-zA-Z0-9''-'\s]{1,40}$"))
                {
                    Error.Content = "Mã số chỉ được là chữ a-zA-Z";
                    textBox1.Focus();
                    return false;
                }
            }
            if (radioButton2.IsChecked == true)
            {
                if (!Regex.IsMatch(textBox1.Text, @"^[a-zA-Z''-'\s]{1,40}$"))
                {
                    Error.Content = "Họ tên chỉ được là chữ a-zA-Z";
                    textBox1.Focus();
                    return false;
                }
            }
            if (radioButton3.IsChecked == true)
            {
                int num4;
                bool IsNumberisDT4 = int.TryParse(textBox1.Text, out num4);
                if (!IsNumberisDT4)
                {
                    Error.Content = "Chứng minh thư chỉ được nhập số ";
                    textBox1.Focus();
                    return false;
                }
                if (int.Parse(textBox1.Text) < 0 || textBox1.Text.Length > 15)
                {
                    Error.Content = "Số chứng minh thư không hợp lệ";
                    textBox1.Focus();
                    return false;
                }
            }
            return true;
        }

    }
}
